<?php
	global $post;

	function theme_recent($atts, $content = null) {
		extract(shortcode_atts(array(
			'title' => '',
			'category' => '',
			'quantity' => '-1',
			'button' => '',
		), $atts));

		if(!$button) $button = 'More';
		
		$wrapper = '<div class="recent_projects">';

		if($title && $title != ''){
			$wrapper .= '<h3 class="section-title">' . $title . '</h3>';
		}else {
			$wrapper .= '<h3 class="section-title">Recent Projects</h3>';
		}
	
		$wrapper .= '<ul class="projects_carousel clearfix">';

		$wrapEnd = '</ul><!--/ .projects-carousel --></div><!--/ .work-carousel-->';

		$query_array_tax = array(
				array(
					'taxonomy' => 'portfolio_category',
					'field' => 'slug',
					'terms' => $category,
					'operator' => 'IN'
				)
			);
			$query_array = array(
				'posts_per_page' => $quantity ,
				'post_type' => 'portfolio',
				'tax_query' => $query_array_tax
			);

			if ($category == '' || $category == 'all'){
				$query_port = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => $quantity ) );
			}else {
				$query_port = new WP_Query( $query_array );
			}
					
		$data_id = 1;

		$slideOutput = '';

		while($query_port->have_posts()){ $query_port->the_post();
			$image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'full', true);

			$page_custom = theme_get_post_custom();
			$video = ( isset($page_custom['_port_popup_link']) ) ? $page_custom['_port_popup_link'] : '';

			$the_cat = get_the_terms( get_the_ID() , 'portfolio_category');
			$categories = '';
			if(is_array($the_cat))
			foreach($the_cat as $cur_term){
				$categories .= $cur_term->slug .' ';
			}

			$projPermalink = get_permalink();

			if ($video){
				$slideOutput .= '<li>
								<div class="border-shadow">
									<figure>
										<a class="prettyPhoto kids_picture" href="'. $video .'" title="'. get_the_title() .'"><img src="'. aq_resize($image[0], 214, 178, true) .'" width="214" height="178" alt="" /></a>
									</figure>
								</div>
								<h1 class="title">'. get_the_title() .'</h1>
								<p>'. the_excerpt_max_charlength(80, false) .'</p>
								<footer class="aligncenter">
									<a href="'. get_permalink() .'" class="button medium button-style1">'. $button .'</a>
								</footer>
							</li>';
			}else {
				$slideOutput .= '<li>
								<div class="border-shadow">
									<figure>
										<a class="prettyPhoto kids_picture" href="'. $image[0] .'" title="'. get_the_title() .'"><img src="'. aq_resize($image[0], 214, 178, true) .'" width="214" height="178" alt="" /></a>
									</figure>
								</div>
								<h1 class="title">'. get_the_title() .'</h1>
								<p>'. the_excerpt_max_charlength(80, false) .'</p>
								<footer class="aligncenter">
									<a href="'. get_permalink() .'" class="button medium button-style1">'. $button .'</a>
								</footer>
							</li>';
			}

			$data_id++;
		} // end while
		wp_reset_postdata();

			$to_return = $wrapper . $slideOutput . $wrapEnd;
			
			return $to_return;
	}	
	add_shortcode('recent_projects', 'theme_recent');
?>